package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.bin.david.form.core.SmartTable
import com.bin.david.form.data.style.FontStyle
import com.bin.david.form.data.style.LineStyle
import com.bin.david.form.data.table.MapTableData
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.ActivityGroupNowAllPersonBinding
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import com.google.gson.Gson
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import org.json.JSONArray
import org.json.JSONObject

/**
 * @Author      : hxw
 * @Date        : 2025/1/17 17:16
 * @Describe    : 报名人员查询
 */
class ActivityGroupNowAllPerson : BaseActivity<OrgRaceViewModel, ActivityGroupNowAllPersonBinding>() {

    private lateinit var tBody: JSONArray
    private lateinit var tHead: JSONArray

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.Applicant_inquiry)

        initSmartTable()

        val raceId = intent.getIntExtra("raceId", 0).toString()
        showLoading(getString(R.string.loading))
        mViewModel.getAppTeamSignList(raceId)
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取团体赛报名列表 */
        mViewModel.getAppTeamSignListResult.observe(this) {
            refreshUi(it)
        }
    }

    private fun refreshUi(it: Any?) {
        if (it != null) {
            val data = Gson().toJson(it)
            val result = JSONObject(data.toString())
            if (result.has("dynamicTable")) {
                val dynamicTableBody = result.getJSONObject("dynamicTable")
                tHead = dynamicTableBody.getJSONArray("tHead")
                tBody = dynamicTableBody.getJSONArray("tBody")
                if (tBody.length() != 0 && tHead.length() != 0) {
                    setSmartTableData()
                    showEmptyView(false)
                } else {
                    showEmptyView(true)
                }
            } else {
                showEmptyView(true)
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }

    private fun setSmartTableData() {
        val list1 = ArrayList<Any>()
        var value: String
        for (i in 0 until tBody.length()) {
            val map = LinkedHashMap<String, String>()
            for (j in 0 until tHead.length()) {
                val dataHead = tHead[j].toString()
                val dataBody = tBody[i].toString()
                val key = JSONObject(dataHead).getString("hName")
                val valueStr = JSONObject(dataHead).getString("hKey")

                value = JSONObject(dataBody).getString(valueStr)
                when (valueStr) {
                    "ringId" -> value = Util.check2FormatRingId(value)
                }
                map[key] = value
            }
            list1.add(map)
        }
        val mapData = MapTableData.create(getString(R.string.Group_grouping), list1)
        mapData.columns[0].isFixed = true
        mapData.columns[1].isFixed = true
        mDatabind.smartTable.tableData = mapData
    }

    private fun initSmartTable() {
        val data = mDatabind.smartTable as SmartTable<Any>
        val fontStyle = FontStyle(this, 16, ContextCompat.getColor(this, R.color.black))
        val contentStyle = FontStyle(this, 15, ContextCompat.getColor(this, R.color.comm_text_gray_dark))
        val lineStyle = LineStyle()
        lineStyle.color = ContextCompat.getColor(this, R.color.color_99CCFF)
        data.config.columnTitleStyle = fontStyle
        data.config.contentStyle = contentStyle
        data.config.setColumnTitleGridStyle(lineStyle)
        data.config.setContentGridStyle(lineStyle)
        data.config.setHorizontalPadding(20)
            .setVerticalPadding(15)
            .setColumnTitleVerticalPadding(20)
            .setSequenceHorizontalPadding(20)
            .setSequenceVerticalPadding(20)
            .setShowTableTitle(false) // 去除标题
            .setShowXSequence(false)
            .setShowYSequence(false)
            .isFixedYSequence = true
    }

    private fun showEmptyView(isEmpty: Boolean) {
        mDatabind.includeEmptyView.llEmptyData.isVisible = isEmpty
        mDatabind.smartTable.isVisible = !isEmpty
    }

}